package com.example.sociamedia.interaction.controller;

import com.example.sociamedia.interaction.dto.NotificationRequest;
import com.example.sociamedia.interaction.dto.NotificationResponse;
import com.example.sociamedia.interaction.entity.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {
    private final NotificationService notificationService;

    @Autowired
    public NotificationController(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    @PostMapping("")
    public ResponseEntity<NotificationResponse> addNotification(@RequestBody NotificationRequest request) {
        NotificationResponse resp = notificationService.addNotification(request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @GetMapping("")
    public ResponseEntity<List<Notification>> getNotifications(@RequestParam Long userId, @RequestParam(required = false, defaultValue = "false") boolean onlyUnread) {
        return ResponseEntity.ok(notificationService.getNotifications(userId, onlyUnread));
    }

    @PostMapping("/read/{notificationId}")
    public ResponseEntity<NotificationResponse> markAsRead(@RequestParam Long userId, @PathVariable Long notificationId) {
        NotificationResponse resp = notificationService.markAsRead(userId, notificationId);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @PostMapping("/readall")
    public ResponseEntity<NotificationResponse> markAllAsRead(@RequestParam Long userId) {
        NotificationResponse resp = notificationService.markAllAsRead(userId);
        return new ResponseEntity<>(resp, HttpStatus.OK);
    }

    @GetMapping("/unread/count")
    public ResponseEntity<Integer> countUnread(@RequestParam Long userId) {
        return ResponseEntity.ok(notificationService.countUnread(userId));
    }
} 